package com.sm.controller;

import com.sm.model.DataCloudLibrary;
import com.sm.model.User;
import com.sm.service.DataCloudLibraryService;
import com.sm.service.MyVipService;
import com.sm.service.UserService;
import com.sm.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;

@Controller
@RequestMapping("/dataCloud")
public class DataCloudLibraryController {

    @Autowired
    private DataCloudLibraryService dataCloudLibraryService;
    @Autowired
    private UserService userService;
    @Autowired
    private MyVipService myVipService;

    /**
     * 资料云库详情页（预览资料云库）
     */
    @RequestMapping("/dataInfo")
    @ResponseBody
    public Msg dataInfo(Integer userId,Integer dataId){
        if (userId == null || dataId == null){
            return Msg.fail().add("msg","参数错误");
        }
        //先获取用户的最高级别身份
        Integer topType = myVipService.getTopType(userId);
        DataCloudLibrary dataCloudLibrary = dataCloudLibraryService.selectByPrimaryKey(dataId);
        if (ObjectUtils.isEmpty(dataCloudLibrary)){
            return Msg.fail().add("msg","資料不存在");
        }
        String newDocUrl = dataCloudLibrary.getDocUrl().substring(0, dataCloudLibrary.getDocUrl().lastIndexOf(".") + 1)+"png";
        dataCloudLibrary.setDocUrl(newDocUrl);
        Integer downloadFlag = 0;
        if (topType != null&&topType >= 6){
            //用户是超级会员及以上，可下载
            downloadFlag = 1;
        }else{
            //用户不是超级会员
            //再判断云库资料的价格是否为0
            if (dataCloudLibrary.getPrice()==null) {
                downloadFlag = 1;
            }
            int i = dataCloudLibrary.getPrice().compareTo(BigDecimal.ZERO);
            if (i != 1){
               downloadFlag = 1;
            }
            //判断此用户是否购买了此资料如果没有不能下载
        }
        updateReadNum(dataCloudLibrary,1);
        return Msg.success().add("dataCloudLibrary",dataCloudLibrary).add("downloadFlag",downloadFlag);
    }


    //将资料的阅读次数累加
    public void updateReadNum(DataCloudLibrary dataCloudLibrary,Integer plus){
        int newReadNum = dataCloudLibrary.getReadNum()+plus;
        DataCloudLibrary library = new DataCloudLibrary();
        library.setReadNum(newReadNum);
        library.setId(dataCloudLibrary.getId());
        library.setUpdateTime(new Date());
        dataCloudLibraryService.updateDataCloud(library);
    }

    /**
     * 资料云库分享
     */
    @RequestMapping("/shareDataCloud")
    @ResponseBody
    public Msg shareDataCloud(Integer dataId){
        DataCloudLibrary dataCloudLibrary = dataCloudLibraryService.selectByPrimaryKey(dataId);
        int newShareNum = dataCloudLibrary.getShareNum()+1;
        DataCloudLibrary library = new DataCloudLibrary();
        library.setShareNum(newShareNum);
        library.setId(dataId);
        library.setUpdateTime(new Date());
        dataCloudLibraryService.updateDataCloud(library);
        return Msg.success();
    }

    /**
     * 资料云库下载
     */
    @RequestMapping("/download")
    @ResponseBody
    public Msg download(Integer userId,Integer dataId){
        if (dataId == null||userId == null){
            return Msg.fail().add("msg","参数错误");
        }
        DataCloudLibrary dataCloudLibrary = dataCloudLibraryService.selectByPrimaryKey(dataId);
        Integer topType = myVipService.getTopType(userId);
        if (topType!=null&&topType>=6){
            updateDownloadNum(dataCloudLibrary,1);
            return Msg.success(dataCloudLibrary);
        }else {
            if (dataCloudLibrary.getPrice()==null) {
                return Msg.fail().add("msg","资料价格不详");
            }
            int i = dataCloudLibrary.getPrice().compareTo(BigDecimal.ZERO);
            if (i != 1){
                updateReadNum(dataCloudLibrary,1);
                return Msg.success(dataCloudLibrary);
            }else{
                //判断此用户是否购买了此资料如果没有跳转支付
                return Msg.pay();
            }
        }
    }

    //修改下载次数
    public void updateDownloadNum(DataCloudLibrary dataCloudLibrary,Integer plus){
        DataCloudLibrary newDataCloudLibrary = new DataCloudLibrary();
        newDataCloudLibrary.setDownloadNum(dataCloudLibrary.getDownloadNum()+plus);
        newDataCloudLibrary.setId(dataCloudLibrary.getId());
        newDataCloudLibrary.setUpdateTime(new Date());
        dataCloudLibraryService.updateDataCloud(newDataCloudLibrary);
    }












}
